package com.et114.components.rule.support;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.et114.components.rule.RuleBaseLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 */
public abstract class AbstractFileRuleBaseLoader implements RuleBaseLoader {

    /**
     * RuleBase
     */
    RuleBase ruleBase = RuleBaseFactory.newRuleBase();

  
    protected String[] ruleFiles;


    protected ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
            this.getClass().getClassLoader());

    public void setRuleFiles(String ruleFiles[]) {
        this.ruleFiles = ruleFiles;
    }


    protected Resource[] getRuleFiles(String[] files) throws IOException {
        List resourceList = new ArrayList();
        for ( int i = 0 ; i < files.length ; i ++ ) {
        	String file = files[  i ] ;
            Resource[] resources = getResources(file);
            resourceList.addAll(Arrays.asList(resources));
        }
        return (Resource[]) resourceList.toArray();
    }

    protected Resource[] getResources(String s) throws IOException {
        return resolver.getResources(s);
    }
}
